﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HK2009.BusinessFacade;
using HK2009.EntityManage;
using HK2009.Common;
using HK2009.EntityQuery;
namespace HK2009.BusinessFacadeHandler
{
    public class BNBookingNoteHandler : IBNBookingNoteHandler
    {
        DataBase db = new DataBase();
        BN_BookingNoteManage bnManage = new BN_BookingNoteManage();
        BN_BookingNoteDtlManage bnDManage = new BN_BookingNoteDtlManage();

        /// <summary>
        /// 根据sql语句同参数插入托运单相关表
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public bool BookingNoteAdd(string sqlStr, SqlParameter[] parms)
        {
           return db.ExecSqlTransaction(sqlStr, parms);
        }

        /// <summary>
        /// 根据sql语句同参数获取客户列表
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public DataTable GetBookingNoteList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);
        }

        /// <summary>
        /// 根据sql语句同参数获取托运单详细列表
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public DataTable GetBookingNoteDetailList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);
        }


        /// <summary>
        /// 根据PKID获取bookingnote的信息
        /// </summary>
        /// <returns></returns>
        public BN_BookingNoteQuery GetBookingNoteByPKID(int id)
        {
            return bnManage.GetBN_BookingNoteQuery(id);
        }

        ///// <summary>
        ///// 根据bookingnote id 获取bookingnotedetail的信息
        ///// </summary>
        ///// <param name="bookingNoteID"></param>
        ///// <returns></returns>
        //public DataTable GetBookingNoteDetailByBookNoteID(int bookingNoteID)
        //{
        //    SqlParameter[] parms = new SqlParameter[1];
        //    parms[0] = new SqlParameter("@BookingNoteID",bookingNoteID);
        //    string sqlStr = "select * from BN_BookingNoteDtl where BookingNoteID=@BookingNoteID order by PKID";
        //}

        /// <summary>
        /// 根据detail　id获取bookingnotedetail信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BN_BookingNoteDtlQuery GetBookingNoteDetailByID(int id)
        {
            return bnDManage.GetBN_BookingNoteDtlQuery(id);
        }

        /// <summary>
        /// 根据sql语句同参数更新主托运单表信息
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public bool BookingNoteUpdate(string sqlStr, SqlParameter[] parms)
        {
            return db.ExecSqlTransaction(sqlStr, parms);
        }

        /// <summary>
        /// 根据sql语句同参数增删改托运单表详细信息
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public bool BookingNoteDetailEdit(string sqlStr, SqlParameter[] parms)
        {
            return db.ExecSqlTransaction(sqlStr, parms);
        }

        /// <summary>
        /// 根据sql语句同参数增删改托运单表和详细表信息
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public bool BookingNoteAndDetailEdit(string sqlStr, SqlParameter[] parms)
        {
            return db.ExecSqlTransaction(sqlStr, parms);
        }

        /// <summary>
        /// 根据PKID删除托运单详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool BookingNoteDetailDelete(int id)
        {
           return  bnDManage.DeleteBN_BookingNoteDtl(id);
        }

        /// <summary>
        /// 根据BookingNoteID更新主表和从表的状态为0
        /// </summary>
        /// <param name="bookingNoteID"></param>
        /// <returns></returns>
        public bool BookingNoteAndDetailUpdateStatus(int bookingNoteID)
        {
            SqlParameter[] parmsU = new SqlParameter[2];
            parmsU[0] = new SqlParameter("@BookingNoteID", bookingNoteID.ToString());
            parmsU[1] = new SqlParameter("@Status", "0");
            string sqlStr = "update BN_BookingNote set Status=@Status where PKID=@BookingNoteID;update BN_BookingNoteDtl set Status=@Status where BookingNoteID=@BookingNoteID";

          return  db.ExecSqlTransaction(sqlStr, parmsU);
        }

        /// <summary>
        /// 根据BookingNoteID获取托运单详细数目
        /// </summary>
        /// <param name="booknoteID"></param>
        /// <returns></returns>
        public int GetBookingNoteTotal(int booknoteID)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@BookingNoteID", booknoteID.ToString());
            string sqlStr = "select Count(*) as countDetail from BN_BookingNoteDtl where BookingNoteID = @BookingNoteID";
            DataTable dt = db.RunSqlTab(sqlStr, parms);
            if (dt != null && dt.Rows.Count > 0)
            {
                return int.Parse(dt.Rows[0]["countDetail"].ToString());
            }
            else
            {
                return 0;
            }

        }

        /// <summary>
        /// 根据BookingNoteID获取托运单详细已完成的数目
        /// </summary>
        /// <param name="booknoteID"></param>
        /// <returns></returns>
        public int GetBookingNoteCompleteTotal(int booknoteID)
        {
            SqlParameter[] parms = new SqlParameter[2];
            parms[0] = new SqlParameter("@BookingNoteID", booknoteID.ToString());
            parms[1] = new SqlParameter("@Status",Enums.BookingNoteDtlStatus.MATECONFIRM.ToString("d"));
            string sqlStr = "select Count(*) as countDetail from BN_BookingNoteDtl where BookingNoteID = @BookingNoteID and Status=@Status";
            DataTable dt = db.RunSqlTab(sqlStr, parms);
            if (dt != null && dt.Rows.Count > 0)
            {
                return int.Parse(dt.Rows[0]["countDetail"].ToString());
            }
            else
            {
                return 0;
            }
        }

        public DataTable GetBookingNoteDtlByBookingNoteID(int bookingNoteID)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@BookingNoteID",bookingNoteID.ToString());
            DataTable dt = bnDManage.GetBN_BookingNoteDtlList("BookingNoteID=@BookingNoteID",parms);
            return dt;
        }
       
    }
}
